<?php
namespace app\controller;

use think\facade\View;

use app\controller\Base;
use app\model\Article as ArticleModel;
use app\model\Tag as TagModel;
use app\model\TagArticle as TagArticle;

class Article  extends Base
{
    public function index()
    {
        $tops = ArticleModel::getTop(6);
        
        View::assign('tops', $tops);

        $list = ArticleModel::queryPage(['status'=>1]);

        View::assign('list', $list);
        
        return View::fetch();
    }

    public function read($id=0)
    {
        if ($id == 0) {
            return $this->notFound("文章不存在");
        }

        $data = ArticleModel::getOne($id);
        
        if ($data == null) {
            return $this->notFound("文章不存在");
        }

        // $nextprev = ArticleModel::getNextPrev($id, $data->cid);
        $nextprev = ArticleModel::getNextPrev($id);

        // 覆盖Base seo
        $this->system['title'] = $data->title . '-' . $this->system['title'];
        $this->system['key'] = $data->key . '-' . $this->system['key'];
        $this->system['des'] = $data->des;

        View::assign('system', $this->system); 

        return View::fetch('', [
            'data'     => $data,
            'nextprev' => $nextprev
        ]);
    }

    public function tags($name = '')
    {
        if ($name == '') {
            $list = null;
        }

        $tag = TagModel::where('tagname', $name)->limit(1)->find();
        
        if ($tag == null) {
            $list = null;
        } else {
            $list = TagArticle::queryList($tag->tagid);
        }
        
        View::assign('list', $list);

        View::assign('tagname', $name);
        
        return View::fetch();
    }

    public function archive($year = '', $month = '')
    {
        $yearMonth = $year . '-' . $month;

        $list = ArticleModel::queryPage(['yearMonth' => $yearMonth]);

        $this->system['title'] = "Archive for " . $yearMonth;

        View::assign('system',  $this->system); // 覆盖Base seo

        View::assign('list', $list);
        
        View::assign('year', $year);

        View::assign('month', $month);

        return View::fetch();
    }
}
